<?php

class SdbController extends Controller
{
	/**
	 * This is the default 'index' action that is invoked
	 * when an action is not explicitly requested by users.
	 */
	/**
	 * @var ESDBConnection the sdbcon for establishing Amazon SimpleDB connection. Defaults to empty string.
	 */
	private $_sdbConn;
	private $_domain = 'us-customs-tariff-2011';
	private $_host = 'sdb.ap-southeast-1.amazonaws.com';
	protected function beforeAction($action) {
		$session = new CHttpSession ();
		$session->open ();
		if ($session->get ( 'ESDBConnection.accessKey' ) && $session->get ( 'ESDBConnection.secretKey' )) {
			$this->_sdbConn=new ESDBConnection($session->get ( 'ESDBConnection.accessKey' ),$session->get ( 'ESDBConnection.secretKey' ),$this->_host);
			$this->_sdbConn->cainfo=YiiBase::getPathOfAlias('ext.yii-simpledb').'/'.'ca-bundle.crt';
		} else if (($sdb = Yii::app()->getComponent('sdb'))!==null) {
			$this->_sdbConn = $sdb;
		}
		$session->close ();
		return parent::beforeAction ( $action );
	}

	public function actionIndex($reset=false,$accessKey='',$secretKey='')
	{
		$session = new CHttpSession();
		$session->open();
		if ($reset) {
			$session->clear();
		} else {
			$session['ESDBConnection.accessKey'] = $accessKey;
			$session['ESDBConnection.secretKey'] = $secretKey;
		}
		$session->close();
		echo CVarDumper::dumpAsString($session->toArray(),10,true);
		echo CVarDumper::dumpAsString($this->_sdbConn,10,true);
		Yii::app()->end();
	}
	public function actionDomain($mode='list', $domain=null) 
	{
		switch ($mode) {
			case 'list':
				$res = $this->_sdbConn->listDomain();
			break;
			case 'create':
				$res = $this->_sdbConn->createDomain($domain);
			break;
			case 'delete':
				$res = $this->_sdbConn->deleteDomain($domain);
			break;
			case 'metadata':
				$res = $this->_sdbConn->domainMetadata($domain);
			break;
		}
		echo CVarDumper::dumpAsString($res,10,true);
		Yii::app()->end();
	}
	public function actionItem($mode='list', $domain=null, $limit=10, $offset=0, $itemname=null, $csv=null) 
	{
		switch ($mode) {
			case 'query':
				$res = $this->_sdbConn->createCommand()
				->select('itemName()')
				->from($domain)
				->where(array('like','brand','%xenia%'))
				->limit($limit,$offset)
				->query();
			break;
			case 'list':
				$res = $this->_sdbConn->getAttributes($domain, $itemname);
			break;
			case 'insert':
				$attributes = array(
					'bbm'=>array('value'=>array('premium','pertamax')),'brand'=>array('value'=>'avega'),'produsen'=>'hyundai','jenis'=>'city car'
				);
				$items=array($attributes);
				$items = array(
					array('brand'=>'i40','produsen'=>'hyundai','bbm'=>array('premium','pertamax'),'jenis'=>'city car'),
					'h1'=>array('brand'=>'h1','produsen'=>'hyundai','bbm'=>'bensin','jenis'=>'lux van')
				);
				function autonum($attr) {
					return $attr['brand'].'_gamu';
				}
				$res = $this->_sdbConn->createCommand()
				->insert($domain, $items, 'autonum');
			break;
			case 'update':
				$expected=array('bbm'=>array('value'=>'premium'));
				$res = $this->_sdbConn->createCommand()
				->update($domain, $attributes,$itemname, $expected);
			break;
			case 'delete':
				$res = $this->_sdbConn->createCommand()->delete($domain,array('like','brand','%xenia%'));
			break;
		}
		echo CVarDumper::dumpAsString($res,10,true);
		Yii::app()->end();
	}
	public function actionUsitc() {
		$count = Yii::app()->db->createCommand()->select('count(*)')->from('tariff_2011')->queryAll();
		$count = current($count[0]);
		
		$rows = Yii::app()->db->createCommand()->select()->from('tariff_2011')->limit(25,0)->queryAll();
		die(CVarDumper::dumpAsString($rows,10,true));
		$this->_sdbConn->createCommand();
	}
}